﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;
using System.Data;

namespace HZ.Supervise.Web.Hospital
{
    public partial class YibaoTest :BasePage
    { 
         
        IExecuteContext iec = HZ.Supervise.BLLFactory.ExecuteContext();
        DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dt = iec.GetYiBaoList(null);
                bindTrees(dt);
            }
        }

        #region 将根节点绑定，同时调用SetChildren方法递归实现子节点的绑定
        /// <summary>
        /// 将根节点绑定，同时调用SetChildren方法递归实现子节点的绑定
        /// </summary>
        /// <param name="ds"></param>
        public void bindTrees(DataTable dt)
        {
            //( YiBaoID, YiBaoName, ParentID )
            DataRow[] datarow = dt.Select("ParentID = '0'");//筛选出根节点
            foreach (DataRow dr in datarow)
            {
                TreeNode tn = new TreeNode();
                tn.Text = dr["YiBaoName"].ToString();
                tn.Value = dr["YiBaoID"].ToString();
                TreeView1.Nodes.Add(tn);//将根节点绑定到TreeView
                tn.Target = "mainFrame";//连接地址指向主框架页
                tn.NavigateUrl = "Detail.aspx?entryGuid=" + tn.Value;
                tn.Expanded = false;
                SetChildren(tn, dr["YiBaoID"].ToString());//调用SelChildren实现递归绑定子节点
            }
        }
        #endregion
        #region 递归实现子节点的绑定
        /// <summary>
        /// 递归实现子节点的绑定
        /// </summary>
        /// <param name="tn">根节点</param>
        /// <param name="id">根节点id</param>
        public void SetChildren(TreeNode tn, string guid)
        {
            DataRow[] datarow = dt.Select("ParentID ='" + guid + "'");
            foreach (DataRow dr in datarow)
            {
                TreeNode ctn = new TreeNode();
                ctn.Text = dr["YiBaoName"].ToString();
                ctn.Value = dr["YiBaoID"].ToString();
                ctn.Target = "mainFrame";
                ctn.NavigateUrl = "Detail.aspx?entryGuid=" + ctn.Value;
                tn.ChildNodes.Add(ctn);
                ctn.Expanded = false;
                SetChildren(ctn, dr["YiBaoID"].ToString());//递归
            }
        }
        #endregion


         

        
    }

}

